const cliProgress = require('cli-progress');
const colors = require('colors');

// 创建一个多进度条容器
const multiBar = new cliProgress.MultiBar({
    clearOnComplete: false, // 完成后不清空进度条
    hideCursor: true,
    format: '{bar} | {taskName} | {percentage}% | {value}/{total} | ETA: {eta_formatted}'
}, cliProgress.Presets.shades_grey);

// 定义多个任务
const tasks = [
    { name: '任务 A', total: 100, current: 0, speed: 0 },
    { name: '任务 B', total: 150, current: 0, speed: 0 },
    { name: '任务 C', total: 80, current: 0, speed: 0 }
];

// 为每个任务创建独立的进度条
const bars = tasks.map(task => {
    return multiBar.create(task.total, task.current, { taskName: colors.yellow(task.name) });
});

// 模拟任务进行
let completedTasks = 0;
const interval = setInterval(() => {
    tasks.forEach((task, index) => {
        if (task.current < task.total) {
            task.current += Math.floor(Math.random() * 5) + 1; // 随机增加进度
            if (task.current > task.total) {
                task.current = task.total; // 确保不超过总数
            }
            bars[index].update(task.current, { taskName: colors.yellow(task.name) });
        } else if (!task.completed) {
            task.completed = true;
            completedTasks++;
            bars[index].update(task.total, { taskName: colors.green(task.name + ' (完成)') }); // 任务完成时改变颜色
        }
    });

    if (completedTasks === tasks.length) {
        multiBar.stop(); // 所有任务完成后停止多进度条容器
        console.log('\n' + colors.green('所有任务完成！'));
        clearInterval(interval);
    }
}, 100);

